package com.bondar.task4;

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReference;

public class LockFree {

	private final AtomicReference<BigInteger> value;

	public LockFree() {
		value = new AtomicReference<BigInteger>( new BigInteger("1") );
	}

	/**
	 * Return series: 1, 2, 4, 8, 16, ...
	 * 
	 * @return
	 */
	public BigInteger next() {
		BigInteger old;
		do {
			old = value.get();
		} while (!value.compareAndSet(old, old.add(old)));
		return old;
			
	}

}
